import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { join } from 'path'
// https://vite.dev/config/

function resolve(dir: string) {
  return join(__dirname, dir)
}
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  if (command === 'serve') {
    return {
      plugins: [vue()],
      resolve: {
        alias: [
          { find: '@', replacement: resolve('src') },
          { find: 'components', replacement: resolve('src/components/') }
        ],
        extensions: ['.js', '.ts', '.json', '.tsx', '.jsx']
      },
      define: {
        _APP_ENV_: env._APP_ENV_
      },
      server: {
        port: 9527,
        open: true
      },
    }
  } else {
    return {
      plugins: [vue()],
      resolve: {
        alias: [
          { find: '@', replacement: resolve('src') },
          { find: 'components', replacement: resolve('src/components/') }
        ],
        extensions: ['.js', '.ts', '.json', '.tsx', '.jsx']
      },
      define: {
        _APP_ENV_: env._APP_ENV_
      }
    }
  }
})
